package Bank;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;  //卡类


/**银行账户类
 * 用于存储每个用户的银行账户信息
 * 信息包括开户日期，金额，交易记录
 * @date	2014.8.17
 *
 */
public class Account {
	Scanner scan=new Scanner(System.in);
	static long ACCOUNT_ID;
	

	private long account_id;
	private long customer_id;
	private String account_type;
	private Date account_dateofopening;
	private float account_balance;
	static private int count =0;
	static  int TRANSACTION_ID=0;
	private Transaction[] THArray = new Transaction[5];
	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	
	static public int getCount(){
		return count;
	}
	public long getAccount_id() {
		return account_id;
	}
	public void setAccount_id(long account_id) {
		this.account_id = account_id;
		count++;                    //银行卡计数 
	}
	public long getCustomer_id() {
		return customer_id;
	}
	public void setCustomer_id(long customer_id) {
		this.customer_id = customer_id;
	}
	public String getAccount_type() {
		return account_type;
	}
	public void setAccount_type(String account_type) {
		this.account_type = account_type;
	}
	public double getAccount_balance() {
		return account_balance;
	}
	public void setAccount_balance(float account_balance) {
		this.account_balance = account_balance;
	}
	public Date getAccount_dateofopening() {
		return account_dateofopening;
	}
	public void setAccount_dateofopening(Date account_dateofopening) {
		this.account_dateofopening = account_dateofopening;
	}
	
	/**
	 * 查询余额
	 */
	public void checkBalance(){ 
	
		    	try {
		    	    
		    		String Query="Select ACCOUNT_BALANCE from ACCOUNT_TABLE where ACCOUNT_ID="+ACCOUNT_ID;
		    		
		    		ResultSet rs =new ConnDB().getrs(Query);
		    		float s = 0;
		    		while(rs.next()){
		    		 s=rs.getFloat("ACCOUNT_BALANCE");
		    		}
		    		System.out.println("ACCOUNT_BALANCE is:"+s);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					//e.printStackTrace();
				}
	}
	
	/**
	 * 取款方法
	 */
	public void withdraw(){ 
		
		float withdrawmenoy=0.0f;//定义取钱变量
		
		System.out.println("Enter your withdraw menoy:");    
     try{
    	//输入钱的金额
	     withdrawmenoy=scan.nextFloat();
	     System.out.println("Success. You withdraw "+withdrawmenoy+" menoy");
     }catch(Exception e){
    	 System.out.println("Error!Please enter number!!");
    	 scan.nextLine();
         withdraw();
     }
     try{
    	//取出ACCOUNT_BALANCE中的余额
	     String Query="Select ACCOUNT_BALANCE from ACCOUNT_TABLE where ACCOUNT_ID="+ACCOUNT_ID;
	     ResultSet rs =new ConnDB().getrs(Query);    	
  		while(rs.next()){
  			account_balance=rs.getFloat("ACCOUNT_BALANCE");
  		}
	     //取钱数与余额比对
	 	if(withdrawmenoy>account_balance)
			System.out.println("You have no much menoy.");
	    else{
	    	account_balance=account_balance-withdrawmenoy;	    	
	    	Date date = new Date();
	    	
	    	 
	    	//更新ACCOUNT_BALANCE中的余额
			String Update="update  ACCOUNT_TABLE set ACCOUNT_BALANCE="+account_balance+"where ACCOUNT_ID="+ACCOUNT_ID;
			 new ConnDB().uprs(Update);
			
			
			//添加一条存取记录。
			//TRANSACTION_ID++;
			String  num="select TRANSACTION_ID from  TRANSACTION_TABLE";
			
		    ResultSet res3=new ConnDB().getrs(num);	
		    while(res3.next()){
				
		    	TRANSACTION_ID=res3.getInt("TRANSACTION_ID")+1;
				//System.out.println(	account_balance);    
			}
			Update="insert into TRANSACTION_TABLE(TRANSACTION_ID,TRANSACTION_DATE,TRANSACTION_AMOUNT,TRANSACTION_TYPE,ACCOUNT_ID) "
					+ "values("+TRANSACTION_ID+",to_date ('"+sdf.format(date)+"','YYYY-MM-DD'),"+withdrawmenoy+",'withdraw',"+ACCOUNT_ID+")";
 
			new ConnDB().uprs(Update);
       }
     }catch(SQLException e){
    	 System.out.println("Error!JDBC IS ERROR!!");
          withdraw();
     }
	}
	
	
	/**
	 * 存款方法
	 */
	public void deposit(){ 
		
		float depositmenoy=0.0f;//定义存款金额
		System.out.println("Enter your deposit menoy");
		try{
			depositmenoy=scan.nextFloat();
			System.out.println("Success. You deposit "+depositmenoy+" menoy");
		}catch(Exception e){
			System.out.println("Error!!Please enter number!!");			
			scan.nextLine();
			deposit();
		}
	
	    	Date date = new Date();	    	
	     	
	    	try {
	    		//取出ACCOUNT_BALANCE中的余额
	    		
	    		 String Query="Select ACCOUNT_BALANCE from ACCOUNT_TABLE where ACCOUNT_ID="+ACCOUNT_ID;
	    	     ResultSet rs =new ConnDB().getrs(Query);
	    		while(rs.next()){
	    			  account_balance=rs.getFloat("ACCOUNT_BALANCE");
	    		}
	    		//将存款记录存入TRANSACTION_TABLE
	    		//TRANSACTION_ID++;
	    		String  num="select TRANSACTION_ID from  TRANSACTION_TABLE";
				
			    ResultSet res3=new ConnDB().getrs(num);	
			    while(res3.next()){
					
			    	TRANSACTION_ID=res3.getInt("TRANSACTION_ID")+1;
					//System.out.println(	account_balance);    
				}
	    		String Update="insert into TRANSACTION_TABLE(TRANSACTION_ID,TRANSACTION_DATE,TRANSACTION_AMOUNT,TRANSACTION_TYPE,ACCOUNT_ID) "
						+ "values("+TRANSACTION_ID+",to_date ('"+sdf.format(date)+"','YYYY-MM-DD'),"+depositmenoy+",'deposit',"+ACCOUNT_ID+")";
	    		 new ConnDB().uprs(Update);
	    		account_balance+=depositmenoy;
	    		//更新ACCOUNT_BALANCE中的余额
	    		Update="update  ACCOUNT_TABLE set ACCOUNT_BALANCE="+account_balance+"where ACCOUNT_ID="+ACCOUNT_ID;
	    		 new ConnDB().uprs(Update);
				
				
			} catch (SQLException e) {
				System.out.println("Error!JDBC IS ERROR!!");
				deposit();
			}
		

	}


	/**
	 * 打印交易记录，只打印最近5次的记录
	 */
    public void viewTransactions(){ 
    	
    	try {
    		//查询交易记录并打印
    		String Query="Select * from TRANSACTION_TABLE where ACCOUNT_ID="+ACCOUNT_ID+"order by TRANSACTION_ID desc";		
    		ResultSet rs = new ConnDB().getrs(Query);
    		int i=0;
    		int a=new ConnDB().uprs(Query);
    		if(a==0){
    			System.out.println("Transactions is null!!");
    		}
    		while(rs.next() && i<5){
    			System.out.println("TRANSACTION_DATE: "+rs.getDate("TRANSACTION_DATE")
    					+"\tTRANSACTION_AMOUNT: "+rs.getFloat("TRANSACTION_AMOUNT")
    					+"\tTRANSACTION_TYPE: "+rs.getString("TRANSACTION_TYPE")
    					+"\tACCOUNT_ID: "+rs.getInt("ACCOUNT_ID"));
    			i++;
    		}
		} catch (SQLException e) {
			//e.printStackTrace();
		}
    }
    
    /**
     * 显示银行卡信息
     */
    public void show(){
    	
    	try {
    		//查询并显示银行卡信息
    		String Query="Select * from ACCOUNT_TABLE where ACCOUNT_ID="+ACCOUNT_ID;
    		ResultSet rs = new ConnDB().getrs(Query);
    		while(rs.next()){
    			System.out.println("ACCOUNT_ID: "+rs.getInt("ACCOUNT_ID")
    					+"\tCUSTOMER_ID: "+rs.getString("CUSTOMER_ID")
    					+"\tACCOUNT_TYPE: "+rs.getString("ACCOUNT_TYPE")
    					+"\tACCOUNT_BALANCE: "+rs.getString("ACCOUNT_BALANCE")
    					+"\tACCOUNT_DATEOFOPENING: "+rs.getString("ACCOUNT_DATEOFOPENING"));
    		}
		} catch (SQLException e) {
			e.printStackTrace();
		}
    }
    
}
